package kpi.java_training.testing.education;

/**
 * Created with IntelliJ IDEA.
 * User: Vladislav
 * Date: 13.11.13
 * Time: 11:02
 * To change this template use File | Settings | File Templates.
 */
public class TypeScore {
    public final int BASIC_LEVEL = 2;
    public final int MAX_AMOUNT_OF_QUESTIONS = 3; // Enough amount of question answered to change level
    private String type;
    private int score;
    private int maxScore;
    private int currentLevel;
    private int answeredQuestions;
    public TypeScore(String type, int score, int maxScore) {
        this.type = type;
        this.score = score;
        this.maxScore = maxScore;
        currentLevel = BASIC_LEVEL;
        answeredQuestions = 0;
    }
    public String getType() {
        return this.type;
    }
    public int getCurrentLevel() {
        return this.currentLevel;
    }
    public void newQuestionAnswered(TypeScore typeScore) {
        this.score += typeScore.score;
        this.maxScore += typeScore.maxScore;
        answeredQuestions++;
        if (answeredQuestions >= MAX_AMOUNT_OF_QUESTIONS) {
            changeLevel();
        }

    }
    public void newQuestionAnswered(boolean isRightAnswer) {
        if(isRightAnswer) {
            score++;
        }
        maxScore++;
        answeredQuestions++;
        if (answeredQuestions >= MAX_AMOUNT_OF_QUESTIONS) {
            changeLevel();
        }
    }
    private void changeLevel() {
        if (getWinPercent() > 0.7) {
            currentLevel++;
            resetScore();
        }
        if (getWinPercent() < 0.3) {
            if(currentLevel > 1) {
                currentLevel--;
            }
            resetScore();
        }
    }
    private void resetScore() {
        answeredQuestions = 0;
        score = 0;
        maxScore = 0;
    }
    public double getWinPercent() {
        double doubleMaxScore = (double)maxScore;
        double doubleScore = (double)score;
        double percent = doubleScore/doubleMaxScore;
        return percent;
    }
}
